package com.ztools.pc.service.impl.sys;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ztools.common.model.pc.sys.Role;
import com.ztools.common.util.common.PageUtil;
import com.ztools.common.util.common.ServiceException;
import com.ztools.common.util.pc.RedisUtil;
import com.ztools.pc.async.RedisAsync;
import com.ztools.pc.mapper.sys.RoleMapper;
import com.ztools.pc.service.sys.RoleService;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author:一口香菜
 * @Date: 2023/3/4  11:51
 * @Version 1.0
 */
@Transactional
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {


    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private RedisAsync redisAsync;

    @Override
    public Page<Role> findList(PageUtil<Role> pageUtil) {
        List<OrderItem> orders = new ArrayList<>();
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>(pageUtil.getParams());
        Page<Role> pages = new Page<>(pageUtil.getPage(), pageUtil.getSize());
        // 排序数据  StrUtil.toUnderlineCase huTools将驼峰装换为下划线格式
        orders.add(new OrderItem(StrUtil.toUnderlineCase(pageUtil.getSortName()), "asc".equals(pageUtil.getSortOrder())));
        pages.setOrders(orders);
        Page<Role> userPage = page(pages, queryWrapper);
        // 字典翻译
        userPage.setRecords(dictionary(userPage.getRecords()));
        return userPage;
    }

    @Override
    public List<Role> findByObject(Role role) {
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>(role);
        return list(queryWrapper);
    }

    @Override
    public List<Role> findByIds(List<Long> ids) {
        return super.listByIds(ids);
    }

    @Override
    public boolean delete(Role role) {
        boolean b = false;
        Role roleById = getById(role);
        String content = stringRedisTemplate.opsForValue().get(RedisUtil.ADMIN_CONTENT) == null ? "admin" : stringRedisTemplate.opsForValue().get(RedisUtil.ADMIN_CONTENT);
        if (roleById.getRoleName().contains(content) || roleById.getRemark().contains(content)) {
            throw new ServiceException("管理员账号不能被删除");
        }
        b = removeById(role);
        if (b) {
            redisAsync.doAfterPermissionChange();
        }
        return b;
    }

    @Override
    public boolean update(Role role) {
        boolean b = false;
        Role roleById = getById(role);
        b = updateById(role);
        return b;
    }


    /**
     * 字典翻译
     *
     * @param roleList roleList
     * @return
     */
    public List<Role> dictionary(List<Role> roleList) {
        for (Role role : roleList) {
            dictionary(role);
        }
        return roleList;
    }

    public void dictionary(Role user) {

    }

}
